package com.fengye.middleware.rabbitmq.boot.demo.object;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 *
 * @author sky
 * @date 2021年06月09日 11:22 上午
 */
@Component
@Slf4j
public class ObjectConsumer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${mq.object.info.queue.name:object_queue_name}")
    private String objectQueueName;

    /**
     * 接受类型接受对象类型的参数，前提是需要生产者在发送消息时指定消息的类型
     *
     * messageProperties.setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, Person.class);
     * @param person
     */
    @RabbitListener(queues = {"${mq.object.info.queue.name:object_queue_name}"}, containerFactory = "singleListenerContainer")
    public void consumerMsg(@Payload Person person) {
        try {
            log.info("对象消息模型：消费者-接收消息：{}", person);
        } catch (Exception e) {
            log.error("对象消息模型：消费者-接收消息异常：", e.fillInStackTrace());
        }
    }
}
